package com.huodongrili.calender;

import java.util.Map;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.huodongrili.calender.bg.LoadWeatherTask;
import com.huodongrili.calender.bg.ScheduleService;
import com.huodongrili.calender.data.Constant;
import com.huodongrili.calender.data.DataPreferences;
import com.huodongrili.calender.data.SortData;
import com.huodongrili.calender.network.FeedbackOper;
import com.huodongrili.calender.widget.FrameDialog;

public class SettingActivity extends CalenderFragmentActivity {

	private DataPreferences mDataPre;
	private CheckBox mWifiCb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_setting);

		setPageTitleData(true);

		mDataPre = DataPreferences.getInstance(getApplicationContext());


		mWifiCb = (CheckBox) findViewById(R.id.cb_wifi);
		
		TextView tv = (TextView)findViewById(R.id.about_title);
		tv.setText("@"+getResources().getString(R.string.about_title));

		ImageView iSend = (ImageView) findViewById(R.id.send_btn);
		final EditText iContent = (EditText) findViewById(R.id.send_content);

		RelativeLayout wifiPane = (RelativeLayout) findViewById(R.id.wifi_pane);
		RelativeLayout aboutPane = (RelativeLayout) findViewById(R.id.about_pane);
		final CheckBox mAboutNav = (CheckBox) findViewById(R.id.cb_exp);
		final ScrollView mAboutSloor = (ScrollView) findViewById(R.id.about_ds);
				


		mWifiCb.setChecked(mDataPre.isWifiSync());

		aboutPane.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//showAboutDlg();
				boolean old = mAboutNav.isChecked();
				mAboutNav.setChecked(!old);
				
				if(!old){
					mAboutSloor.setVisibility(View.VISIBLE);
				}else{
					mAboutSloor.setVisibility(View.GONE);
				}
				
				
			}
		});

		wifiPane.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setWifiSync();
			}
		});

		iSend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				String content = iContent.getText().toString();
				if (content == null || content.length() <= 0) {
					return;
				}

				/*
				 * FeedbackData fdata = new
				 * FeedbackData(getApplicationContext());
				 * fdata.addFeedback(content, System.currentTimeMillis());
				 */

				new SendTask().execute(content);

				iContent.setText("");
				
				((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
				
				Toast.makeText(SettingActivity.this, R.string.send_ok,
						Toast.LENGTH_SHORT).show();

			}
		});

	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	private void setWifiSync() {
		boolean wifiStaus = mWifiCb.isChecked();
		boolean nowStatus = !wifiStaus;
		mDataPre.setWifiSync(nowStatus);
		mWifiCb.setChecked(nowStatus);
		
		Intent i = new Intent(this,ScheduleService.class);
		
		if(nowStatus){
			startService(i);
		}else{
			stopService(i);
		}
		
	}

	/*private void showAboutDlg() {
		FrameDialog dlg = new FrameDialog(this);
		dlg.setTitle(R.string.about_title);
		
		String msg = "@"+getResources().getString(R.string.about_detail);
		
		dlg.setMessage(msg);
		dlg.show();
	}*/

	private void showSelectCityDlg() {

		final FrameDialog dlg = new FrameDialog(this);

		LayoutInflater mInflater = LayoutInflater.from(this);
		ListView listView = (ListView) mInflater.inflate(
				R.layout.layout_custom_list, null);

		final String[] panels = getResources().getStringArray(R.array.city);

		final String[] codes = getResources().getStringArray(R.array.city_code);

		listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.dlg_list_single_choice_item, R.id.tv_check, panels);

		listView.setAdapter(adapter);

		final String currentCode = mDataPre.getCityCode();

		if (currentCode.equals(Constant.CODE_BJ)) {
			listView.setItemChecked(0, true);
		} else if (currentCode.equals(Constant.CODE_SH)) {
			listView.setItemChecked(1, true);
		}

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				/*String code = codes[position];
				mDataPre.setCityCode(code);
				mCityBtn.setText(panels[position]);

				if (!currentCode.equals(code)) {

					sendBroadcast(new Intent(
							Constant.ACTION_SETTING_LOCAL_CHANGED));

					new LoadWeatherTask(getApplicationContext()).execute();

				}*/

				dlg.dismiss();
			}

		});

		dlg.setTitle(R.string.select_city_title);
		dlg.setCustomView(listView);
		dlg.show();
	}

	private class SendTask extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... params) {

			String feedStr = params[0];

			FeedbackOper feedback = new FeedbackOper(getApplicationContext());
			feedback.toOperFeedback(feedStr);

			return null;
		}
	}

}
